var dataBase = (function(e){
    var DB;

    /*-Client---------------------------------------------------------------------------------------*/
    e.insertClient = function(data, action){
        var insertClient, insertPaymentTerms, insertInvoicingPolicy;
        insertClient = "INSERT INTO Clients (ClientCode, Name, StreetAddress," +
        "City, States, CountryID, ZipCode, Email1, Email2, Phone1, Phone2, Phone3, Phone4," +
        "Fax, Note, WebSite, VATNumber, GeneralInfo, CurrencyID, TimeZone) VALUES (?, ?, " +
            "?, ?, ?, ?,?, ?, ?, ?, ?, ?,?, ?, ?, ?,?, ?, ?, ?)";
        insertPaymentTerms="INSERT INTO PaymentTerms( ClientID, MinimumFee," +
            "InvoiceDateType, IDTValue1, IDTValue2, IDTValue3, IDTValue4, Note) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        insertInvoicingPolicy="INSERT INTO InvoicingPolicy(ClientID, InvoiceType, " +
            "AfterTotal, WaitDays, DaysWait) VALUES (?, ?, ?, ?, ?)";
        DB.db.transaction(function (tx){
            tx.executeSql(insertClient,[data.ClientCode, data.Name, data.StreetAddress, data.City, data.State, data.CountryID,
                data.ZipCode, data.Email1, data.Email2, data.Phone1, data.Phone2, data.Phone3, data.Phone4, data.Fax, data.Note,
                data.WebSite, data.VATNumber, data.GeneralInfo, data.CurrencyID, data.TimeZone],
                function (tx, results){
                        if(data.PaymentTerms !== undefined){
                            tx.executeSql(insertPaymentTerms,[results.insertId , data.PaymentTerms.MinimumFee, data.PaymentTerms.InvoiceDateType,
                                data.PaymentTerms.IDTValue1, data.PaymentTerms.IDTValue2, data.PaymentTerms.IDTValue3, data.PaymentTerms.IDTValue4,
                                data.PaymentTerms.Note],function (tx){
                                if(data.InvoicingPolicy !== undefined){
                                    tx.executeSql(insertInvoicingPolicy,[results.insertId , data.InvoicingPolicy.InvoiceType,
                                        data.InvoicingPolicy.AfterTotal, data.InvoicingPolicy.Wait, data.InvoicingPolicy.Days],function(){ action();},DB.errorCB());
                                }
                            },DB.errorCB);
                        }

            },DB.errorCB);
        }, DB.errorCB, DB.successCB);
    };

    e.editClient = function(data, action){
        var editClients, editPaymentTerms, editInvoicingPolicy;
        editClients="UPDATE Clients SET ClientCode = ?, Name = ?, StreetAddress = ?, City = ?, States = ?, CountryID = ?" +
            ", ZipCode = ?, Email1 = ?, Email2 = ?, Phone1= ?, Phone2 = ?, Phone3 = ?, Phone4 = ?, Fax = ?, Note = ?" +
            ", WebSite = ?, VATNumber = ?, GeneralInfo = ?, CurrencyID = ?, TimeZone = ? WHERE ClientID = ?";
        editPaymentTerms="UPDATE PaymentTerms SET MinimumFee = ?," +
            "InvoiceDateType = ?, IDTValue1 = ?, IDTValue2 = ?, IDTValue3 = ?, IDTValue4 = ?, Note = ? WHERE ClientID = ?";
        editInvoicingPolicy="UPDATE InvoicingPolicy SET InvoiceType = ?, " +
            ",AfterTotal = ?, WaitDays = ?, DaysWait = ? WHERE ClientID = ?";
        DB.db.transaction(function(tx){
            tx.executeSql(editClients, [data.ClientCode, data.Name, data.StreetAddress, data.City, data.State, data.CountryID,
                data.ZipCode, data.Email1, data.Email2, data.Phone1, data.Phone2, data.Phone3, data.Phone4, data.Fax, data.Note,
                data.WebSite, data.VATNumber, data.GeneralInfo, data.CurrencyID, data.TimeZone, data.ClientID],{}, DB.errorCB);
            tx.executeSql(editPaymentTerms, [data.PaymentTerms.MinimumFee,
                data.PaymentTerms.AfterTotal, data.PaymentTerms.Wait, data.PaymentTerms.Days, data.ClientID ],{}, DB.errorCB);
            tx.executeSql(editInvoicingPolicy, [data.InvoicingPolicy.InvoiceType, data.InvoicingPolicy.InvoiceDateType,
                data.InvoicingPolicy.IDTValue1, data.InvoicingPolicy.IDTValue2, data.InvoicingPolicy.IDTValue3, data.InvoicingPolicy.IDTValue4,
                data.InvoicingPolicy.Note, data.ClientID],{}, DB.errorCB);
            action();
        }, DB.errorCB, DB.successCB);

    };

    e.deleteClient = function(clientID, action){
        var deleteClient, deletePaymentTerms, deleteInvoicingPolicy;
        deleteClient = "DELETE FROM Clients WHERE ClientID = ?";
        deletePaymentTerms = "DELETE FROM PaymentTerms WHERE ClientID = ?";
        deleteInvoicingPolicy = "DELETE FROM InvoicingPolicy WHERE ClientID = ?";
        DB.db.transaction(function(tx){
            tx.executeSql(deleteClient,[clientID],{}, DB.errorCB);
            tx.executeSql(deletePaymentTerms,[clientID],{}, DB.errorCB);
            tx.executeSql(deleteInvoicingPolicy,[clientID],{}, DB.errorCB);
        },DB.errorCB, DB.successCB);
        action();
    };

    e.getClientsList = function(action){
        var selectClient, data={};
        selectClient = "SELECT ClientID, Name, TimeZone FROM Clients";
        DB.db.transaction(function (tx){
            tx.executeSql(selectClient,[],function(tx, result){
                var i=0,num = result.rows.length, datas=[];
                for(i=0;i<num;i+=1){
                    data.ClientID = result.rows.item(i).ClientID;
                    data.Name = result.rows.item(i).Name;
                    data.TimeZone = result.rows.item(i).TimeZone;
                    datas.push(data);
                }
                action(datas);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };

    e.getClientByID = function (clientID, action){
        var selectClient, selectPaymentTerms, selectInvoicingPolicy, data={};
        selectClient = "SELECT * FROM Clients WHERE ClientID = ?";
        selectPaymentTerms = "SELECT *  FROM PaymentTerms WHERE ClientID = ?";
        selectInvoicingPolicy = "SELECT *  FROM InvoicingPolicy WHERE ClientID = ?";
        DB.db.transaction(function (tx){
            tx.executeSql(selectClient,[clientID],function(tx, result){
                data.ClientID = result.rows.item(0).ClientID;
                data.Name = result.rows.item(0).Name;
                data.TimeZone = result.rows.item(0).TimeZone;
                data.ClientCode = result.rows.item(0).ClientCode;
                data.StreetAddress = result.rows.item(0).StreetAddress;
                data.City = result.rows.item(0).City;
                data.State = result.rows.item(0).State;
                data.CountryID = result.rows.item(0).CountryID;
                data.ZipCode = result.rows.item(0).ZipCode;
                data.Email1 = result.rows.item(0).Email1;
                data.Email2 = result.rows.item(0).Email2;
                data.Phone1 = result.rows.item(0).Phone1;
                data.Phone2 = result.rows.item(0).Phone2;
                data.Phone3 = result.rows.item(0).Phone3;
                data.Phone4 = result.rows.item(0).Phone4;
                data.Fax = result.rows.item(0).Fax;
                data.Note = result.rows.item(0).Note;
                data.WebSite = result.rows.item(0).WebSite;
                data.VATNumber = result.rows.item(0).VATNumber;
                data.GeneralInfo = result.rows.item(0).GeneralInfo;
                data.CurrencyID = result.rows.item(0).CurrencyID;
                data.PaymentTerms = {};
                data.InvoicingPolicy = {};
                tx.executeSql(selectPaymentTerms,[clientID],function(tx, result){
                    data.PaymentTerms.PaymentTermsID = result.rows.item(0).PaymentTermsID;
                    data.PaymentTerms.MinimumFee = result.rows.item(0).MinimumFee;
                    data.PaymentTerms.InvoiceDateType = result.rows.item(0).InvoiceDateType;
                    data.PaymentTerms.IDTValue1 = result.rows.item(0).IDTValue1;
                    data.PaymentTerms.IDTValue2 = result.rows.item(0).IDTValue2;
                    data.PaymentTerms.IDTValue3 = result.rows.item(0).IDTValue3;
                    data.PaymentTerms.IDTValue4 = result.rows.item(0).IDTValue4;
                    data.PaymentTerms.Note = result.rows.item(0).Note;
                    tx.executeSql(selectInvoicingPolicy,[clientID],function(tx, result){
                        data.InvoicingPolicy.InvoicingPolicyID = result.rows.item(0).InvoicingPolicyID;
                        data.InvoicingPolicy.InvoiceType = result.rows.item(0).InvoiceType;
                        data.InvoicingPolicy.AfterTotal = result.rows.item(0).AfterTotal;
                        data.InvoicingPolicy.Wait = result.rows.item(0).WaitDays;
                        data.InvoicingPolicy.Days = result.rows.item(0).DaysWait;
                        action(data);
                    }, DB.errorCB);
                }, DB.errorCB);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*Client Contacts----------------------------------------------------------------------------------*/
    e.getClientContacts = function(clientID, action){
        var selectContacts;
        selectContacts = "SELECT ContactID, Salutation, Name, Position, Email1 FROM Contacts WHERE ClientID=?";
        DB.db.transaction( function (tx){
            tx.executeSql(selectContacts,[clientID], function (tx, result){
                var i= 0, num = result.rows.length, data=[], item={};
                for(i;i<num;i+=1){
                    item.ContactID = result.rows.item(i).ContactID;
                    item.Salutation = result.rows.item(i).Salutation;
                    item.Name = result.rows.item(i).Name;
                    item.Position = result.rows.item(i).Position;
                    item.Email1 = result.rows.item(i).Email1;
                    data.push(item);
                }
                action(data);
            }, DB.errorCB)
        }, DB.errorCB, DB.successCB)
    };

	e.insertClientContact = function (data, action){
		var insertContact;
		insertContact = "INSERT INTO Contacts (ClientID, Salutation, Name, Position, Email1, Email2, Phone1, Phone2, Phone3, Phone4, Fax, Note) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
		DB.db.transaction( function (tx){
			tx.executeSql(insertContact,[data.ClientID, data.Salutation, data.Name, data.Position, data.Email1, data.Email2, data.Phone1,data.Phone2, data.Phone3, data.Phone4, data.Fax, data.Note ],
				function (){ action(); },DB.errorCB);
		}, DB.errorCB, DB.successCB);
	};
	
	e.editClientContact = function (data, action) {
	  	var updateContact;
	  	updateContact = "UPDATE Contacts SET Salutation=?, Name=?, Position=?, Email1=?, Email2=?, Phone1=?, Phone2=?, Phone3=?, Phone4=?, Fax=?, Note=? WHERE ContactID=?";
	  	DB.db.transaction( function (tx){
			tx.executeSql(updateContact,[data.Salutation, data.Name, data.Position, data.Email1, data.Email2, data.Phone1,data.Phone2, data.Phone3, data.Phone4, data.Fax, data.Note, data.ContactID ],
				function (){ action(); },DB.errorCB);
		}, DB.errorCB, DB.successCB);
	};
	
	e.getClientContactByID = function (contactID, action) {
	  	var selectContact;
	  	selectContact = "SELECT * FROM Contacts WHERE ContactID=?";
	  	DB.db.transaction( function (tx){
            tx.executeSql(selectContact,[contactID], function (tx, result){
                var item={};
                item.ContactID 	= result.rows.item(0).ContactID;
                item.Salutation = result.rows.item(0).Salutation;
                item.Name 		= result.rows.item(0).Name;
                item.Position 	= result.rows.item(0).Position;
                item.Email1 	= result.rows.item(0).Email1;
                item.Email2 	= result.rows.item(0).Email2;
 				item.Phone1 	= result.rows.item(0).Phone1;
 				item.Phone2 	= result.rows.item(0).Phone2;
 				item.Phone3 	= result.rows.item(0).Phone3;
 				item.Phone4 	= result.rows.item(0).Phone4;
 				item.Fax	 	= result.rows.item(0).Fax;
 				item.Note	 	= result.rows.item(0).Note;
                action(item);
            }, DB.errorCB)
        }, DB.errorCB, DB.successCB)
	}
    /*-------------------------------------------------------------------------------------------------*/
    /*-Prices-----------------------------------------------------------------------------------*/
    e.insertPrices = function(data, action){
        var insertPrices;
        insertPrices="INSERT INTO Prices (ClientID, GroupsOfServicesID, ServicesID, VolumeUnitsID, Price) VALUEs(?, ?, ?, ?, ?)";
        DB.db.transaction(function(tx){
            tx.executeSql(insertPrices,[data.ClientID, data.GroupsOfServicesID, data.ServicesID, data.VolumeUnitsID, data.Price], function (tx){
                action();
            },DB.errorCB);
        },DB.errorCB, DB.successCB);
    };

    e.editPrices = function (data, action){
        var editPrices;
        editPrices =  "UPDATE Prices SET GroupsOfServicesID=?, ServicesID=?, VolumeUnitsID=?, Price=? WHERE PriceID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(editPrices,[data.GroupsOfServicesID, data.ServicesID, data.VolumeUnitsID, data.Price, data.PriceID], function (){action();},DB.errorCB);
        },DB.errorCB, DB.successCB);
    };

    e.getPricesByID = function( pricesID, action){
        var getPricesByID;
        getPricesByID = "SELECT * FROM Prices WHERE PriceID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(getPricesByID,[pricesID],function(tx, result){
                var item={};
                item.PriceID            = result.rows.item(0).PriceID;
                item.GroupsOfServicesID = result.rows.item(0).GroupsOfServicesID;
                item.ServicesID         = result.rows.item(0).ServicesID;
                item.VolumeUnitsID      = result.rows.item(0).VolumeUnitsID;
                item.Price              = result.rows.item(0).Price;
                action(item);
            }, DB.errorCB);
        }, DB.errorCB, DB.successCB);
    };

    e.getPricesList = function(clientID, action){
        var getPricesByID;
        getPricesByID = "SELECT p.PriceID, s.Services, vu.Name, p.Price, crr.Currency FROM Prices as p left join Services as s on p.ServicesID = s.ServicesID" +
            " left join VolumeUnits as vu on p.VolumeUnitsID = vu.VolumeUnitsID left join Clients as c on p.ClientID = c.ClientID" +
            " left join Currencies as crr on c.CurrencyID = crr.CurrencyID WHERE p.ClientID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(getPricesByID,[clientID],function(tx, result){
                var data=[],i= 0,num = result.rows.length, item={};
                for(i;i<num;i+=1){
                    item={};
                    item.PriceID    = result.rows.item(i).PriceID;
                    item.Services   = result.rows.item(i).Services;
                    item.Name       = result.rows.item(i).Name;
                    item.Price      = result.rows.item(i).Price;
                    item.Currency   = result.rows.item(i).Currency
                    data.push(item);
                }
                action(data);
            }, DB.errorCB);
        }, DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*-Projects----------------------------------------------------------------------------------------*/
    e.insertProject = function(data, action){
        var insertProject;
        insertProject = "INSERT INTO Projects(ClientID, " +
            "Name, ClientRef, GroupsOfServicesID, ServicesID, VolumeUnitsID, Price, " +
            "ProjectVolume, PriceType, AssignedDate, DeadlineDate, CompletedDate, Completed, Specialization, " +
            "Instruction, WorkNotes, Priority, Status) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        DB.db.transaction(function(tx){
            tx.executeSql(insertProject,[data.ClientID, data.Name, data.ClientRef, data.GroupsOfServicesID,
            data.ServicesID, data.VolumeUnitsID, data.Price, data.ProjectVolume, data.PriceType, data.AssignedDate,
            data.DeadlineDate, data.CompletedDate, data.Completed, data.Specialization, data.Instruction,
            data.WorkNotes, data.Priority, data.Status],function(tx){
                action();
            },DB.errorCB);
        }, DB.errorCB, DB.successCB);
    };

    e.editProject = function(data, action){
        var editProject;
        editProject = "UPDATE Projects SET ClientID=?, " +
            "Name=?, ClientRef=?, GroupsOfServicesID=?, ServicesID=?, VolumeUnitsID=?, Price=?, " +
            "ProjectVolume=?, PriceType=?, AssignedDate=?, DeadlineDate=?, CompletedDate=?, Completed=?, Specialization=?, " +
            "Instruction=?, WorkNotes=?, Priority=?, Status=? WHERE ProjectID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(insertProject,[data.ClientID, data.Name, data.ClientRef, data.GroupsOfServicesID,
                data.ServicesID, data.VolumeUnitsID, data.Price, data.ProjectVolume, data.PriceType, data.AssignedDate,
                data.DeadlineDate, data.CompletedDate, data.Completed, data.Specialization, data.Instruction,
                data.WorkNotes, data.Priority, data.Status, data.ProjectID],function(tx){
                action();
            },DB.errorCB);
        }, DB.errorCB, DB.successCB);
    };

    e.getProjectByID = function(projectID, action){
        var getProjectByID;
        getProjectByID = "SELECT * FROM Projects WHERE ProjectID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(getProjectByID,[projectID], function(tx, result){
                var item={};
                if(result.rows.length>0){
                    item.ProjectID = result.rows.item(0).ProjectID;
                    item.Name = result.rows.item(0).Name;
                    item.ClientRef = result.rows.item(0).ClientRef;
                    item.GroupsOfServicesID = result.rows.item(0).GroupsOfServicesID;
                    item.ServicesID = result.rows.item(0).ServicesID;
                    item.VolumeUnitsID = result.rows.item(0).VolumeUnitsID;
                    item.Price = result.rows.item(0).Price;
                    item.ProjectVolume = result.rows.item(0).ProjectVolume;
                    item.AssignedDate = result.rows.item(0).AssignedDate;
                    item.DeadlineDate = result.rows.item(0).DeadlineDate;
                    item.CompletedDate = result.rows.item(0).CompletedDate;
                    item.Completed = result.rows.item(0).Completed;
                    item.Specialization = result.rows.item(0).Specialization;
                    item.Instruction = result.rows.item(0).Instruction;
                    item.WorkNotes = result.rows.item(0).WorkNotes;
                    item.Priority = result.rows.item(0).Priority;
                    item.Status = result.rows.item(0).Status;
                    action(item);
                }
            },DB.errorCB);

        }, DB.errorCB, DB.successCB);
    };

    e.getProjectList = function(clientID, action){
        var getProjectList;
        getProjectList="SELECT * FROM Projects WHERE ClientID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(getProjectList,[clientID],function(tx, result){
                var i= 0,num = result.rows.length, data=[];
                for(i;i<num;i+=1){
                    var item={};
                    item.ProjectID = result.rows.item(i).ProjectID;
                    item.Name = result.rows.item(i).Name;
                    data.push(item);
                }
                action(data);
            }, DB.errorCB);
        }, DB.errorCB, DB.successCB);
    }

    /*-------------------------------------------------------------------------------------------------*/
    /*-Currences---------------------------------------------------------------------------------------*/
    e.getCurrencyList = function (action){
        var selectCurrency, dataList=[];
        selectCurrency = "SELECT * FROM Currencies";
        DB.db.transaction(function (tx){
            tx.executeSql(selectCurrency,[],function(tx, result){
                var i= 0, num = result.rows.length;
                for(i;i<num;i+=1){
                    data={};
                    data.CurrencyID = result.rows.item(i).CurrencyID;
                    data.Currency = result.rows.item(i).Currency;
                    dataList.push(data);
                }
                action(dataList);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*-Countries---------------------------------------------------------------------------------------*/
    e.getCountriesList = function (action){
        var selectCountries, dataList=[];
        selectCountries = "SELECT * FROM Countries";
        DB.db.transaction(function (tx){
            tx.executeSql(selectCountries,[],function(tx, result){
                var i= 0, num = result.rows.length;
                for(i;i<num;i+=1){
                    data = {};
                    data.CountryID = result.rows.item(i).CountryID;
                    data.Name = result.rows.item(i).Name;
                    dataList.push(data);
                }
                action(dataList);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*-Groups Of Services------------------------------------------------------------------------------*/
    e.getGroupsOfServicesList = function(action){
        var selectGroupsOfServices;
        selectGroupsOfServices = "SELECT * FROM GroupsOfServices";
        DB.db.transaction(function(tx){
            tx.executeSql(selectGroupsOfServices, [], function (tx, result){
                var i= 0, num = result.rows.length, data=[], item={};
                for(i;i<num;i+=1){
                    item={};
                    item.GroupsOfServicesID = result.rows.item(i).GroupsOfServicesID;
                    item.GroupName = result.rows.item(i).GroupName;
                    data.push(item);
                }
                action(data);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*-Services----------------------------------------------------------------------------------------*/
    e.getServicesList = function(groupsOfServicesID, action){
        var selectServices;
        selectServices = "SELECT * FROM Services WHERE GroupsOfServicesID=?";
        DB.db.transaction(function(tx){
            tx.executeSql(selectServices, [groupsOfServicesID], function (tx, result){
                var i= 0, num = result.rows.length, data=[], item={};
                for(i;i<num;i+=1){
                    item={};
                    item.ServicesID = result.rows.item(i).ServicesID;
                    item.Services = result.rows.item(i).Services;
                    data.push(item);
                }
                action(data);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    /*-Volume Units------------------------------------------------------------------------------------*/
    e.getVolumeUnitsList = function(action){
        var selectVolumeUnits;
        selectVolumeUnits = "SELECT * FROM VolumeUnits";
        DB.db.transaction(function(tx){
            tx.executeSql(selectVolumeUnits, [], function (tx, result){
                var i= 0, num = result.rows.length, data=[], item={};
                for(i;i<num;i+=1){
                    item={};
                    item.VolumeUnitsID = result.rows.item(i).VolumeUnitsID;
                    item.Name = result.rows.item(i).Name;
                    data.push(item);
                }
                action(data);
            }, DB.errorCB);
        },DB.errorCB, DB.successCB);
    };
    /*-------------------------------------------------------------------------------------------------*/
    DB = {
        db: null,

        init: function () {
            DB.db = window.openDatabase("TPMS", "1.0", "TPMC", 400000000);
            DB.db.transaction(DB.createTables, DB.errorCB, DB.successCB);
        },
        createTables: function(tx){
            tx.executeSql('CREATE TABLE IF NOT EXISTS GroupsOfServices ( GroupsOfServicesID INTEGER PRIMARY KEY AUTOINCREMENT, GroupName TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Services ( ServicesID INTEGER PRIMARY KEY AUTOINCREMENT, GroupsOfServicesID INTEGER, Services TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS VolumeUnits ( VolumeUnitsID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Currencies ( CurrencyID INTEGER PRIMARY KEY AUTOINCREMENT, Currency TEXT, Description TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Countries ( CountryID INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS PaymentMethods ( PaymentMethodsID INTEGER PRIMARY KEY AUTOINCREMENT, PaymentMethods TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Clients ( ClientID INTEGER PRIMARY KEY AUTOINCREMENT, ClientCode TEXT, Name TEXT, StreetAddress TEXT,' +
                'City TEXT, States TEXT, CountryID INTEGER, ZipCode TEXT, Email1 TEXT, Email2 TEXT, Phone1 TEXT, Phone2 TEXT, Phone3 TEXT, Phone4 TEXT,' +
                'Fax TEXT, Note TEXT, WebSite TEXT, VATNumber TEXT, GeneralInfo TEXT, CurrencyID INTEGER, TimeZone INTEGER)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS PaymentTerms ( PaymentTermsID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, MinimumFee INTEGER,' +
                'InvoiceDateType INTEGER, IDTValue1 INTEGER, IDTValue2 INTEGER, IDTValue3 INTEGER, IDTValue4 INTEGER, Note TEXT )');
            tx.executeSql('CREATE TABLE IF NOT EXISTS InvoicingPolicy ( InvoicingPolicyID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, InvoiceType TEXT, ' +
                'AfterTotal TEXT, WaitDays TEXT, DaysWait INTEGER)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Contacts ( ContactID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, Salutation TEXT, ' +
                'Name TEXT, Position TEXT, Email1 TEXT, Email2 TEXT, Phone1 TEXT, Phone2 TEXT, Phone3 TEXT, Phone4 TEXT,' +
                'Fax TEXT, Note TEXT )');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Prices ( PriceID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, GroupsOfServicesID INTEGER, ' +
                'ServicesID INTEGER, VolumeUnitsID INTEGER, Price NUMBER )');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Projects ( ProjectID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, ' +
                'Name TEXT, ClientRef TEXT, GroupsOfServicesID INTEGER, ServicesID INTEGER, VolumeUnitsID INTEGER, Price NUMBER, ' +
                'ProjectVolume NUMBER, PriceType INTEGER, AssignedDate TEXT, DeadlineDate TEXT, CompletedDate TEXT, Completed INTEGER, Specialization TEXT, ' +
                'Instruction TEXT, WorkNotes TEXT, Priority TEXT, Status TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Invoices ( InvoiceID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, ' +
                ' Value NUMBER, PaymentMethodsID INTEGER, Description TEXT, Payed NUMBER )');
            tx.executeSql('CREATE TABLE IF NOT EXISTS InvoiceProjects ( InvoiceID INTEGER, ProjectID INTEGER)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS Payments ( PaymentID INTEGER PRIMARY KEY AUTOINCREMENT, ClientID INTEGER, Value NUMBER, Note TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS PaymentInvoices ( PaymentID INTEGER, InvoiceID INTEGER)');
            tx.executeSql('Select * from Countries',[],function (tx, results){
                if(results.rows.length === 0){
                    DB.insertTempData(tx);
                }
            }, DB.errorCB);
        },
        insertTempData:function(tx){
            var insertGroupsOfServices, insertServices, insertVolumeUnits, insertCurrencies, insertCountries, insertPaymentMethods;

            insertGroupsOfServices = "INSERT INTO GroupsOfServices (GroupName) VALUES (?)";
            tx.executeSql(insertGroupsOfServices, ["- None -"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["Consulting"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["DTP Tasks"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["Editing"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["Other"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["Testing"], {}, DB.errorCB);
            tx.executeSql(insertGroupsOfServices, ["Translation"], {}, DB.errorCB);

            insertServices = "INSERT INTO Services (GroupsOfServicesID, Services) VALUES (?, ?)";
            tx.executeSql(insertServices, [2, "Cultural Assessment"], {}, DB.errorCB);
            tx.executeSql(insertServices, [2, "Multilingual Copywriting"], {}, DB.errorCB);
            tx.executeSql(insertServices, [3, "DTP in Adobe FrameMaker"], {}, DB.errorCB);
            tx.executeSql(insertServices, [3, "DTP in Adobe PageMaker"], {}, DB.errorCB);
            tx.executeSql(insertServices, [3, "DTP in Quark XPress"], {}, DB.errorCB);
            tx.executeSql(insertServices, [4, "DTP check"], {}, DB.errorCB);
            tx.executeSql(insertServices, [4, "Implementing corrections"], {}, DB.errorCB);
            tx.executeSql(insertServices, [4, "Proofreading"], {}, DB.errorCB);
            tx.executeSql(insertServices, [1, "- None -"], {}, DB.errorCB);
            tx.executeSql(insertServices, [5, "Additional work"], {}, DB.errorCB);
            tx.executeSql(insertServices, [6, "Testing of Localized Software"], {}, DB.errorCB);
            tx.executeSql(insertServices, [7, "English => French"], {}, DB.errorCB);
            tx.executeSql(insertServices, [7, "French => English"], {}, DB.errorCB);

            insertVolumeUnits = "INSERT INTO VolumeUnits (Name) VALUES (?)";
            tx.executeSql(insertVolumeUnits, ["- None -"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["characters (with spaces)"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["characters (without spaces)"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["hours"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["lines (55 characters)"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["pages (1800 characters)"], {}, DB.errorCB);
            tx.executeSql(insertVolumeUnits, ["words"], {}, DB.errorCB);

            insertCurrencies = "INSERT INTO Currencies (Currency, Description) VALUES (?, ?)";
            tx.executeSql(insertCurrencies, ["AUD", "Australian Dollar"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["CAD", "Canadian Dollar"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["CHF", "Swiss Franc"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["EUR", "Euro"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["GBP", "British Pounds"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["JPY", "Japanese Yen"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["SEK", "Swedish Krona"], {}, DB.errorCB);
            tx.executeSql(insertCurrencies, ["USD", "US Dollars"], {}, DB.errorCB);

            insertCountries = "INSERT INTO Countries (Name) VALUES (?)";
            tx.executeSql(insertCountries, ["Afghanistan"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Albania"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Algeria"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Andorra"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Angola"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Antigua and Barbuda"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Argentina"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Armenia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Australia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Austria"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Azerbaijan"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bahamas"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bahrain"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bangladesh"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Barbados"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Belarus"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Belgium"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Belize"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Benin"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bhutan"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bolivia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bosnia and Herzegovina"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Botswana"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Brazil"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Brunei"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Bulgaria"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Burkina Faso"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Burma"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Burundi"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cambodia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cameroon"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Canada"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cape Verde"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Central African Republic"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Chad"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Chile"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["China"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Colombia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Comoros"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Costa Rica"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cote d'Ivoire"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Croatia"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cuba"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Cyprus"], {}, DB.errorCB);
            tx.executeSql(insertCountries, ["Czech Republic"], {}, DB.errorCB);

            insertPaymentMethods = "INSERT INTO PaymentMethods (PaymentMethods) VALUES (?)";
            tx.executeSql(insertPaymentMethods, ["- None -"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Bank wire transfer"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Cash"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Check"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Credit/Debit card"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["MoneyGram International"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Moneybookers"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["PayPal"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Webmoney"], {}, DB.errorCB);
            tx.executeSql(insertPaymentMethods, ["Western Union"], {}, DB.errorCB);

        },
        errorCB: function (err) {
            //alert(err.code);
        },

        successCB: function () {
            //DB.db.transaction(DB.queryDB, DB.errorCB);
        }
    };
    e.init = function (){
        DB.init();
    };
    e.init();
    return e;
}(dataBase || {}));
